package com.haoniu.common.utils.file;

import java.io.*;

/**
 * 文件处理工具类
 *
 * @author haoniu
 */
public class FileUtils {

    /**
     * 　 * 私有构造方法，防止类的实例化，因为工具类不需要实例化。
     */
    private FileUtils() {
    }

    /**
     * 输出指定文件的byte数组
     *
     * @param filename 文件
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath 文件
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 读取文件内容
     *
     * @param inputStream
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String readFileToString(InputStream inputStream) throws UnsupportedEncodingException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        StringBuffer fileString = new StringBuffer();
        try {
            String readLine = null;
            while ((readLine = bufferedReader.readLine()) != null) {
                fileString.append(readLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedReader.close();
                inputStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return fileString.toString();
    }

    public static void writeFile(String file, String content) {
        // 相对路径，如果没有则要建立一个新的文件
        try {
            File writeName = new File(file);
            // 创建新文件,有同名的文件的话直接覆盖
            writeName.createNewFile();
            try (FileWriter writer = new FileWriter(writeName); BufferedWriter out = new BufferedWriter(writer)) {
                out.write(content);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*public static void main(String[] args) {
        try {
            List<String> list = org.apache.commons.io.FileUtils.readLines(new File("G:\\login.js"), "utf-8");
            System.out.println(list);
            org.apache.commons.io.FileUtils.writeLines(new File("G:\\login2.js"),list,false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
